﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace WemoConnect1
{
    public class ReadRulesCommand : WemoCommand
    {
        string GetCommand(string authority)
        {
            var headerBuilder = new StringBuilder();
            headerBuilder.AppendLine("GET /rules.db HTTP/1.1");
            headerBuilder.AppendLine("Host: " + authority);
            headerBuilder.AppendLine(); // End of HTTP header

            return headerBuilder.ToString();
        }

        protected override void Execute()
        {
            var request = GetCommand(WemoSocket.RemoteEndPoint.ToString());
            SendRequest(request);

            ReceiveRequestState();

            var filePath = "rules_"+ DateTime.Now.ToString("yy_MM_dd_hhmm") +".zip";
            ReceiveAndSaveRules(filePath);
            Debug.WriteLine("\n\nRules saved to " + filePath + "\n\n");
        }

        private void ReceiveAndSaveRules(string filePath)
        {
            var responsebuf = new byte[51200];
            var lenR = WemoSocket.Receive(responsebuf);
            using (var file = File.Create(filePath))
            {
                file.Write(responsebuf, 0, lenR);
            }
        }
    }

    public class SendRulesCommand : WemoCommand
    {
        private string _filePath;

        public SendRulesCommand(string filePath)
        {
            _filePath = filePath;
        }
        string GetCommand(string authority, int len)
        {
            var headerBuilder = new StringBuilder();
            headerBuilder.AppendLine("POST /rules.db HTTP/1.1");
            headerBuilder.AppendLine("Host: " + authority);
            headerBuilder.AppendLine("Accept-Encoding: .gzip,.deflate");
            headerBuilder.AppendLine("Content-type: application/x-www-form-urlencoded");
            headerBuilder.AppendLine("Connection: Keep-Alive");
            headerBuilder.AppendLine("Content-Length: " + len);
            headerBuilder.AppendLine(); // End of HTTP header

            return headerBuilder.ToString();
        }

        protected override void Execute()
        {
            var buffer = new byte[51200];
            var length = GenerateRequest(ref buffer);

            SendRequest(buffer, length);
            ReceiveRequestState();
        }

        private int GenerateRequest(ref byte[] buffer)
        {
            var filebuf = new byte[51200];
            var fileSize = 0;
            using (var file = File.OpenRead(_filePath))
            {
                fileSize = file.Read(filebuf, 0, 51200);
            }

            var command = GetCommand(WemoSocket.RemoteEndPoint.ToString(), fileSize);
            var request = Encoding.UTF8.GetBytes(command);

            Array.Copy(request, buffer, request.Length);
            Array.Copy(filebuf, 0, buffer, request.Length, fileSize);

            return request.Length + fileSize;
        }
    }
}
